/**
 * Created by Administrator on 2017/7/6 0006.
 */
// 添加项目
$('#deploy-project-tabs').find('.deploy-project-tab1').click(function () {
    $('#deploy-project-p1').css('display', 'block');
    $('#deploy-project-p2').css('display', 'none');
    $('#deploy-project-addproject-btn button').text('添加项目');
    $('#deploy-project-modal-header h4').text("添加项目");
    $('#deploy-project-dir').removeAttr('disabled');
    $('#deploy-project-savebtn .btn-save').attr('saveflg', 'addpro');
    $('#deploy-project-savebtn .btn-save').css('display', 'inline-block');
    $('#deploy-project-savebtn .btn-edit-save').css('display', 'none');
});
//创建项目
$('#deploy-project-tabs').find('.deploy-project-tab2').click(function () {
    $('#deploy-project-p1').css('display', 'none');
    $('#deploy-project-p2').css('display', 'block');
    $('#deploy-project-addproject-btn button').text('创建项目');
    $('#deploy-project-modal-header h4').text("创建项目");
    $('#deploy-project-dir').attr('disabled', 'disabled');
    $('#deploy-project-savebtn .btn-save').attr('saveflg', 'createpro');
});

$('#deploy-project-savebtn').delegate('.btn-save', 'click', function () {
    var self = this;
    var projectname = $('#deploy-project-name').val().trim();
    var hostid = $('#deploy-project-host').val();
    var userandgroup = $('.deploy-project-user-multiple').val();
    var saveflgattr = $(self).attr('saveflg');
    var postdata = {
        'projectname': projectname,
        'hostid': hostid,
        'userandgroup': userandgroup,
        'saveflag': 'addpro'
    };
    console.log(saveflgattr);
    if (saveflgattr == 'addpro') {
        //添加项目
        var posturl = '/deploy/addproject/';
        var projectdir = $('#deploy-project-dir').val().trim();
        postdata['projectdir'] = projectdir;
    } else if (saveflgattr == 'createpro') {
        //创建项目
        var posturl = '/deploy/createproject/';
    }
    $.ajax({
        url: posturl,
        type: 'POST',
        data: postdata,
        success: function (data) {
            console.log(data);
            window.location.href = '/deploy/projectindex/';
        }
    });
    $('#deploy-project-modal').modal('hide');
    $('.modal-backdrop').remove()
});

//删除项目
$('.deploy-project-exec').delegate('.deploy-project-delpro', 'click', function () {
    var selfdel = this;
    var selfobj = $(selfdel).parent().parent();
    var proid = $(selfobj).attr('proid');

    $.ajax({
        url: '/deploy/delproject/',
        type: 'POST',
        data: {'projectid': proid},
        success: function (data) {
            console.log(data);
            $(selfobj).remove();
        }
    })

});
// 编辑项目
$('.deploy-project-exec').delegate('.deploy-project-editpro', 'click', function () {
    $('#deploy-project-savebtn .btn-save').css('display', 'none');
    $('#deploy-project-savebtn .btn-edit-save').css('display', 'inline-block');
    var selfedit = this;
    $(selfedit).addClass('editproActive');
    var project_id = $(selfedit).parent().parent().attr('proid');
    var proname = $(selfedit).parent().parent().find('.deploy-project-name').text();
    var prodir = $(selfedit).parent().parent().find('.deploy-project-dir').text();
    var prohost = $(selfedit).parent().parent().find('.deploy-project-host').attr('prohostid');
    var prownerlist = [];
    var prouid = $(selfedit).parent().prev().children().each(function () {
        var prouself = this;
        prownerlist.push($(prouself).text())
    });
    $('#deploy-project-name').val(proname);
    $('#deploy-project-dir').val(prodir);
    $('#deploy-project-host').val(prohost);
    $('.deploy-project-user-multiple').select2('val', prownerlist);

});
$('#deploy-project-savebtn').delegate('.btn-edit-save', 'click', function () {
    var projectname = $('#deploy-project-name').val().trim();
    var hostid = $('#deploy-project-host').val();
    var userandgroup = $('.deploy-project-user-multiple').val();
    var projectdir = $('#deploy-project-dir').val().trim();
    var project_id = $('.deploy-project-exec').find('.editproActive').parent().parent().attr('proid');
    console.log(userandgroup);
    $.ajax({
        url: '/deploy/editproject/',
        type: 'POST',
        data: {
            'projectname': projectname,
            'hostid': hostid,
            'userandgroup': userandgroup,
            'projectdir': projectdir,
            'projectid': project_id
        },
        success: function (data) {
            console.log(data);
            window.location.href = '/deploy/projectindex/';
        }
    });
    $('#deploy-project-modal').modal('hide');
    $('.modal-backdrop').remove()
});


//clear dirty data
$('#deploy-project-modal').on('hidden.bs.modal', function () {
    $('#deploy-project-name').val('');
    $('#deploy-project-dir').val('');
    $('.deploy-project-user-multiple').select2('val', '');
});